home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / lightsout.swf / scripts / frame_2 / DoAction.as
Text File  |  2007-10-01  |  11KB  |  395 lines

  1. function init()
  2. {
  3.    music_sound = new Sound();
  4.    music_sound.attachSound("music");
  5.    light_sound = new Sound();
  6.    light_sound.attachSound("light_sound");
  7.    if(_global.music == undefined || _global.music == "off")
  8.    {
  9.       music_sound.start(0,9999999);
  10.       _global.music = "on";
  11.    }
  12.    music_sound.setVolume(35);
  13.    saveFile = SharedObject.getLocal("Lights_Out_3D");
  14.    generateBackground();
  15.    game = {active:false,tileW:50,currentLayer:2,currentTile:null,currentMap:1,currentScore:0,totalScore:0,Mode:"makeCustom",totalMoves:0,overallMoves:0};
  16.    loadGame();
  17.    saveGame();
  18. }
  19. function generateBackground()
  20. {
  21.    d = 0;
  22.    xp = 10;
  23.    yp = 10;
  24.    _root.attachMovie("empty","backgroundTiles",1);
  25.    y = 0;
  26.    while(y < 4)
  27.    {
  28.       x = 0;
  29.       while(x < 4)
  30.       {
  31.          name = "t_" + y + "_" + x;
  32.          backgroundTiles.attachMovie("menuTile",name,d);
  33.          d++;
  34.          backgroundTiles[name]._x = xp;
  35.          backgroundTiles[name]._y = yp;
  36.          backgroundTiles[name].gotoAndStop("off");
  37.          xp += 110;
  38.          x++;
  39.       }
  40.       yp += 110;
  41.       xp = 10;
  42.       y++;
  43.    }
  44.    backgroundTiles._alpha = 15;
  45.    i = 0;
  46.    while(i < 4)
  47.    {
  48.       y = Math.floor(Math.random() * 7);
  49.       x = Math.floor(Math.random() * 9);
  50.       changeLights(x,y);
  51.       i++;
  52.    }
  53.    wait = setInterval(triggerLights,1000);
  54.    generateMenu();
  55. }
  56. function triggerLights()
  57. {
  58.    y = Math.floor(Math.random() * 4);
  59.    x = Math.floor(Math.random() * 4);
  60.    changeLights(x,y);
  61. }
  62. function changeLights(x, y)
  63. {
  64.    tile1 = backgroundTiles["t_" + y + "_" + x];
  65.    tile2 = backgroundTiles["t_" + (y + 1) + "_" + x];
  66.    tile3 = backgroundTiles["t_" + (y - 1) + "_" + x];
  67.    tile4 = backgroundTiles["t_" + y + "_" + (x + 1)];
  68.    tile5 = backgroundTiles["t_" + y + "_" + (x - 1)];
  69.    i = 1;
  70.    while(i <= 5)
  71.    {
  72.       tile = _root["tile" + i];
  73.       if(tile != undefined)
  74.       {
  75.          frame = tile._currentframe;
  76.          if(frame == 1)
  77.          {
  78.             tile.gotoAndStop("on");
  79.          }
  80.          else
  81.          {
  82.             tile.gotoAndStop("off");
  83.          }
  84.       }
  85.       i++;
  86.    }
  87. }
  88. function generateMenu()
  89. {
  90.    buttons = ["New Game","Continue","Random","Play Custom","Make Custom","How To Play","Highscores","Credits"];
  91.    _root.attachMovie("empty","menuClips",2);
  92.    menuClips.attachMovie("tittle","tittle",200);
  93.    menuClips.tittle._x = Stage.width / 2;
  94.    menuClips.tittle._y = menuClips.tittle._height / 2 + 50;
  95.    i = 0;
  96.    while(i < buttons.length)
  97.    {
  98.       name = buttons[i];
  99.       menuClips.attachMovie("menuButton",name,i);
  100.       menuClips[name]._x = Stage.width / 2;
  101.       menuClips[name]._y = i * 30 + 200;
  102.       menuClips[name].t.text = buttons[i];
  103.       i++;
  104.    }
  105.    _root.attachMovie("error_message","errorMessage",3);
  106.    errorMessage._x = Stage.width / 2;
  107.    errorMessage._y = Stage.height - 20;
  108. }
  109. function saveGame()
  110. {
  111.    saveFile.data.currentMap = game.currentMap;
  112.    saveFile.data.totalScore = game.totalScore;
  113. }
  114. function loadGame()
  115. {
  116.    errorMessage.display_txt.text = "*Looking for saved game*";
  117.    if(saveFile.data.currentMap != undefined)
  118.    {
  119.       errorMessage.display_txt.text = "*Loading saved game*";
  120.       game.currentMap = saveFile.data.currentMap;
  121.       game.totalScore = saveFile.data.totalScore;
  122.       i = 0;
  123.       while(i < 5)
  124.       {
  125.          errorMessage.display_txt.text = "*Looking for custom map data*";
  126.          if(saveFile.data["customMapName" + i] != undefined)
  127.          {
  128.             _root["customMap" + i] = new Object();
  129.             _root["customMap" + i].name = saveFile.data["customMapName" + i];
  130.             _root["customMap" + i].map = saveFile.data["customMapArray" + i];
  131.             errorMessage.display_txt.text = "*Loading Custom Map : " + _root["customMap" + i].name + "*";
  132.             errorMessage.play();
  133.          }
  134.          i++;
  135.       }
  136.       errorMessage.display_txt.text = "*Game data loaded successfully*";
  137.       menuClips.NewGame.toString.text = "Continue";
  138.       loaded = true;
  139.       errorMessage.play();
  140.       menuClips._y = 0;
  141.    }
  142.    else
  143.    {
  144.       errorMessage.display_txt.text = "*No saved game found*";
  145.       errorMessage.play();
  146.       menuClips._y = 0;
  147.    }
  148. }
  149. function setupBoard(quitButton, infoPannel, resetButton, lvl)
  150. {
  151.    if(quitButton != undefined)
  152.    {
  153.       _root.attachMovie("menuButton","Quit",200);
  154.       Quit.t.text = "Quit";
  155.       Quit._x = Quit._width / 2 + 10;
  156.       Quit._y = Quit._height / 2 + 10;
  157.    }
  158.    if(resetButton != undefined)
  159.    {
  160.       _root.attachMovie("menuButton","Reset",201);
  161.       Reset.t.text = "Reset Level";
  162.       Reset._x = Reset._width / 2 + 10;
  163.       Reset._y = Reset._height / 2 + 35;
  164.    }
  165.    if(infoPannel != undefined)
  166.    {
  167.       _root.attachMovie("info","info",202);
  168.       info._x = Stage.width - 10;
  169.       info._y = 10;
  170.       if(game.Mode == "normal")
  171.       {
  172.          info.lvl.text = game.currentMap;
  173.          info.score.text = game.totalScore;
  174.          info.moves.text = game.totalMoves;
  175.       }
  176.       else
  177.       {
  178.          info.lvl.text = lvl;
  179.          info.score.text = "n/a";
  180.          info.moves.text = game.totalMoves;
  181.       }
  182.    }
  183. }
  184. function loadScores()
  185. {
  186.    var _loc6_ = "http://localhost/gameTest2/lightOut_highscores.php?task=read";
  187.    var _loc5_ = new LoadVars();
  188.    _loc5_.onLoad = function(success)
  189.    {
  190.       if(success)
  191.       {
  192.          scoreArray = this.scores;
  193.          scoreArray = scoreArray.split("|");
  194.          var _loc2_ = new Array();
  195.          i = 0;
  196.          while(i < scoreArray.length - 1)
  197.          {
  198.             _loc2_.push({user:scoreArray[i],score:scoreArray[i + 1]});
  199.             i += 2;
  200.          }
  201.          _loc2_.sort(compareNumbers);
  202.          i = 0;
  203.          while(i < _loc2_.length)
  204.          {
  205.             i++;
  206.          }
  207.          displayScores(_loc2_);
  208.       }
  209.    };
  210.    _loc5_.load(_loc6_);
  211. }
  212. function compareNumbers(a, b)
  213. {
  214.    res = (Number(a.score) > Number(b.score)) - (Number(a.score) < Number(b.score));
  215.    return res;
  216. }
  217. function displayScores(scores)
  218. {
  219.    p = 1;
  220.    i = scores.length - 1;
  221.    while(p < 6)
  222.    {
  223.       scores_page["name" + p].text = scores[i].user;
  224.       scores_page["score" + p].text = scores[i].score;
  225.       p++;
  226.       i--;
  227.    }
  228. }
  229. stop();
  230. loaded = false;
  231. init();
  232. menuClips["New Game"].btn.onRelease = function()
  233. {
  234.    removeMovieClip(menuClips);
  235.    game.currentMap = 1;
  236.    game.currentScore = 0;
  237.    game.totalScore = 0;
  238.    game.totalMoves = 0;
  239.    game.Mode = "normal";
  240.    gotoAndStop("game");
  241.    setupBoard(1,1,1);
  242. };
  243. menuClips.Continue.btn.onRelease = function()
  244. {
  245.    removeMovieClip(menuClips);
  246.    game.Mode = "normal";
  247.    setupBoard(1,1,1);
  248.    gotoAndStop("game");
  249. };
  250. menuClips.Random.btn.onRelease = function()
  251. {
  252.    removeMovieClip(menuClips);
  253.    setupBoard(1,1,1);
  254.    game.Mode = "random";
  255.    gotoAndStop("game");
  256. };
  257. menuClips["How To Play"].btn.onRelease = function()
  258. {
  259.    menuClips._y = 500;
  260.    attachMovie("help_page","help_page",4);
  261.    help_page._x = Stage.width / 2;
  262.    help_page._y = Stage.height / 2;
  263.    help_page.btn.onRelease = function()
  264.    {
  265.       removeMovieClip(help_page);
  266.       menuClips._y = 0;
  267.    };
  268. };
  269. menuClips.Highscores.btn.onRelease = function()
  270. {
  271.    menuClips._y = 500;
  272.    attachMovie("scores_page","scores_page",4);
  273.    scores_page._x = Stage.width / 2;
  274.    scores_page._y = Stage.height / 2;
  275.    loadScores();
  276.    scores_page.btn.onRelease = function()
  277.    {
  278.       removeMovieClip(scores_page);
  279.       menuClips._y = 0;
  280.    };
  281. };
  282. menuClips.Credits.btn.onRelease = function()
  283. {
  284.    menuClips._y = 500;
  285.    attachMovie("credits_page","credits_page",4);
  286.    credits_page._x = Stage.width / 2;
  287.    credits_page._y = Stage.height / 2;
  288.    credits_page.btn.onRelease = function()
  289.    {
  290.       removeMovieClip(credits_page);
  291.       menuClips._y = 0;
  292.    };
  293. };
  294. menuClips["Play Custom"].btn.onRelease = function()
  295. {
  296.    menuClips._y = 500;
  297.    _root.attachMovie("empty","customs",5);
  298.    i = 1;
  299.    while(i < 7)
  300.    {
  301.       if(i != 6)
  302.       {
  303.          name = _root["customMap" + i].name;
  304.          customs.attachMovie("menuButton","loadCustom" + i,i);
  305.          customs["loadCustom" + i]._x = Stage.width / 2;
  306.          customs["loadCustom" + i]._y = i * 40 + 160;
  307.          customs["loadCustom" + i].t.text = name;
  308.       }
  309.       else
  310.       {
  311.          customs.attachMovie("menuButton","Back",i);
  312.          customs.Back.t.text = "Back";
  313.          customs.Back._x = Stage.width / 2;
  314.          customs.Back._y = i * 40 + 160;
  315.       }
  316.       i++;
  317.    }
  318.    customs.Back.btn.onRelease = function()
  319.    {
  320.       removeMovieClip(customs);
  321.       menuClips._y = 0;
  322.    };
  323.    customs.loadCustom1.btn.onRelease = function()
  324.    {
  325.       if(customMap1.name != undefined)
  326.       {
  327.          removeMovieClip(customs);
  328.          removeMovieClip(menuClips);
  329.          game.currentMap = customMap1.map;
  330.          game.Mode = "playCustom";
  331.          gotoAndStop("game");
  332.          setupBoard(1,1,1,customMap1.name);
  333.       }
  334.    };
  335.    customs.loadCustom2.btn.onRelease = function()
  336.    {
  337.       if(customMap2.name != undefined)
  338.       {
  339.          removeMovieClip(customs);
  340.          removeMovieClip(menuClips);
  341.          game.currentMap = customMap2.map;
  342.          game.Mode = "playCustom";
  343.          gotoAndStop("game");
  344.          setupBoard(1,1,1,customMap2.name);
  345.       }
  346.    };
  347.    customs.loadCustom3.btn.onRelease = function()
  348.    {
  349.       if(customMap3.name != undefined)
  350.       {
  351.          removeMovieClip(customs);
  352.          removeMovieClip(menuClips);
  353.          game.currentMap = customMap3.map;
  354.          game.Mode = "playCustom";
  355.          gotoAndStop("game");
  356.          setupBoard(1,1,1,customMap3.name);
  357.       }
  358.    };
  359.    customs.loadCustom4.btn.onRelease = function()
  360.    {
  361.       if(customMap4.name != undefined)
  362.       {
  363.          removeMovieClip(customs);
  364.          removeMovieClip(menuClips);
  365.          game.currentMap = customMap4.map;
  366.          game.Mode = "playCustom";
  367.          gotoAndStop("game");
  368.          setupBoard(1,1,1,customMap4.name);
  369.       }
  370.    };
  371.    customs.loadCustom5.btn.onRelease = function()
  372.    {
  373.       if(customMap5.name != undefined)
  374.       {
  375.          removeMovieClip(customs);
  376.          removeMovieClip(menuClips);
  377.          game.currentMap = customMap5.map;
  378.          game.Mode = "playCustom";
  379.          gotoAndStop("game");
  380.          setupBoard(1,1,1,customMap5.name);
  381.       }
  382.    };
  383. };
  384. menuClips["Make Custom"].btn.onRelease = function()
  385. {
  386.    setupBoard(1);
  387.    game.Mode = "makeCustom";
  388.    _root.attachMovie("menuButton","saveCustom",10);
  389.    saveCustom.t.text = "Save Level";
  390.    saveCustom._x = Stage.width / 2;
  391.    saveCustom._y = Stage.height - 60;
  392.    removeMovieClip(menuClips);
  393.    gotoAndStop("game");
  394. };
  395.